package com.xcjh.opencv;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.xcjh.opencv.listen.GlobalKeyListenerExample;
import org.opencv.core.Core;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.awt.*;
import java.util.Scanner;

/**
 * 找到相似图片,并且移动上去
 */
@Component
public class FindSimilarImage {


    /**
     * 最小阈值,越小代表越匹配.
     */
    public static final double MIN = 0.007;

    /**
     * 存储屏幕尺寸
     */
    private static final Dimension DIM;
    /**
     * 自动化对象
     */
    public static Robot robot;
    /**
     * 是否继续执行下去
     */
    public static boolean go = true;

    static {
        System.out.println("初始化");
        /*
         * 加载OpenCV库
         */
        DIM = Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println("屏幕尺寸:" + DIM.getHeight());
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        // 加载OpenCV库
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //初始化并监听键盘
        listener();
    }

    /**
     * 初始化,并且监听键盘
     */
    private static void listener() {
        // 初始化全局键盘钩子
        try {
            System.out.println("开始");
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("Failed to register native hook: " + ex.getMessage());
            System.exit(1);
        }
        // 创建监听器实例
        GlobalKeyListenerExample listener = new GlobalKeyListenerExample();
        // 添加监听器
        GlobalScreen.addNativeKeyListener(listener);
    }

    public static String getScanner(String outStr) {
        System.out.println(outStr);
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        return "";
    }

    /**
     * 监听规则
     * 查询需要调用的规则
     * 调用规则
     * <p>
     * 另外一根路线:
     * 监听到按F8就把go设置false.就中断规则
     *
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception {
    }









}